from menu_button import MenuButton
from panda3d.core import Vec2, Point3, Vec3, Vec4
from direct.gui.DirectGui import *
from pandac.PandaModules import TextNode
'''
	Andrew Oberlin
	Date: March 6, 2012
'''
class Dialog:
	'''
		Constructs a dialog box
	'''
	def __init__(self, parent, name, headerText="Game Menu"):
		self.parent = parent
		self.name = name
		self.dummy = self.parent.base.aspect2d.attachNewNode(name + '_dummy')
		self.dummy.setZ(0.3)
		self.dialogTextNum = 0
		self.__loadBackground()
		self.items = { 'button' : dict(), 'input' : dict()}
		self.headerNode = TextNode(self.name + '_text')
		self.headerNode.setText(headerText)
		self.headerModel = self.dummy.attachNewNode(self.headerNode)
		self.headerModel.setPos(-0.2,0,0.4)
		self.headerModel.setColor(0.3, 1, 0.3, 1)
		self.headerModel.setScale(0.075)
		
	'''
		Loads the background of the dialog box
	'''
	def __loadBackground(self):
		self.container = MenuButton(self.parent, 'container', "models/gui/game_maker/dialog_background", 
			self.clearFocus, Vec3(0,0,0), scale=1, isMovie=False)
		self.container.buttonDummy.reparentTo(self.dummy)
	
	def addButton(self, loc, text, command):
		pos = Vec3(0, 0, .30 - 0.105*loc)
		okItem = MenuButton(self.parent, text, 'models/gui/game_maker/dialog_button', 
			command, pos, text=text, isMovie=False, textScale=Vec3(0.075,0.10,0.050),
			scale=Vec3(0.6,1,0.175), textPos=Vec3(-0.25, 0, -0.025))
		okItem.buttonDummy.reparentTo(self.dummy)
		self.items['button']['ok'] = okItem
	
	def addOkCancel(self, command, only='', cancelCommand=None, cancelText="Cancel", cancelTextScale=None):
		if (not cancelCommand):
			cancelCommand = self.hide
		if (only == 'ok' or only == ''):
			if (only == 'ok'):
				pos = Vec3(-0.05,0,-0.35)
			else:
				pos = Vec3(-0.3,0,-0.35)
			okItem = MenuButton(self.parent, 'ok', 'models/gui/game_maker/dialog_button', 
				command, pos, text="Ok", isMovie=False,
				scale=Vec3(0.3,1,0.15), textPos=Vec3(-0.075, 0, -0.025))
			okItem.buttonDummy.reparentTo(self.dummy)
			self.items['button']['ok'] = okItem
		
		if (only == 'cancel' or only == ''):
			cancelItem = MenuButton(self.parent, 'cancel', 'models/gui/game_maker/dialog_button', 
				cancelCommand, Vec3(0.2,0,-0.35), text=cancelText, isMovie=False,
				scale=Vec3(0.4,1,0.15), textPos=Vec3(-0.150, 0, -0.025), textScale=cancelTextScale)
			cancelItem.buttonDummy.reparentTo(self.dummy)
			self.items['button']['cancel'] = cancelItem
		
	def addInput(self, loc, name, initialText="Enter a filename"):
		input = DirectEntry(initialText=initialText, cursorKeys=True, scale=0.075, 
			focusInCommand=self.focusInput, focusInExtraArgs=[name])
		input.setPos(-0.40, 0, .30 - 0.1*loc)
		input.reparentTo(self.dummy)
		self.items['input'][name] = input
	
	def getText(self, name):
		return self.items['input'][name].get(plain=True)
		
	'''
	
	'''
	def addTextBox(self, text, pos=Vec3(0, 0, 0.05)):
		self.dialogTextNum += 1
		textBox = TextNode('dialog_text' + str(self.dialogTextNum))
		textBox.setText(text)
		textBox.setWordwrap(15)
		textBox.setAlign(TextNode.ACenter)
		textBoxModel = self.dummy.attachNewNode(textBox)
		textBoxModel.setPos(pos)
		textBoxModel.setScale(0.05)
	'''
	
	'''
	def show(self):
		self.dummy.show()
	
	'''
	
	'''
	def hide(self):
		self.dummy.hide()
		
	
	def focusInput(self, name):
		self.items['input'][name].enterText('')
		
	def clearFocus(self):
		for key in self.items['input'].keys():
			self.items['input'][key]['focus'] = 0
	
	def removeNode(self):
		self.headerModel.removeNode()
		for key in self.items.keys():
			for key2 in self.items[key].keys():
				self.items[key][key2].removeNode()
				
		self.container.removeNode()
		self.dummy.removeNode()
		